package com.thyc.utils;


import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 监控模块数据转换工具类
 */
public class JkConvertUtil {

    /**
     * 16进制转字符串
     *
     * @param hex
     * @return
     */
    public static String convertHexToString(String hex) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hex.length() - 1; i += 2) {
            String output = hex.substring(i, (i + 2));
            sb.append((char) Integer.parseInt(output, 16));
        }
        return sb.toString();
    }

    /**
     * 根据返回字符 判断采集模块开启的采集通道 除了前缀$符号  其余数据 为8的标识通道开启状态 为0的为关闭状态
     *
     * @param str
     * @return
     */
    public static List<String> transformationToMap(String str) {
        List<String> channelList = new ArrayList<>();
        String s = StrUtil.removePrefix(str, "$");
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            String string = String.valueOf(chars[i]);
            if (string.equals("8")) {
                channelList.add("C" + (i < 9 ? "0" +((i + 1)) : ((i + 1))));
            }
        }
        return channelList;
    }


    //接收2：将byte[]转为16进制
    public static String bytesToHexString(byte[] src) {
        StringBuffer sb = new StringBuffer("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v).toUpperCase();
            if (hv.length() < 2) {
                sb.append(0);
            }
            sb.append(hv);
            if (i != src.length - 1) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    /**
     * 16进制转换成10进制
     *
     * @param s
     * @return
     */
    public static List<Integer> hexToDecimal(String s) {
        List<Integer> dataList = new ArrayList<>(16);
        for (int i = 0; i < s.length(); i += 2) {
            String str = s.substring(i, (i + 2));
            Integer x = Integer.parseInt(str, 16);
            dataList.add(x);
        }
        return dataList;
    }

}
